package com.jadefashion.mobile;

import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.health.resources.SMSUtils;
import com.jadefashion.health.resources.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.concurrent.TimeUnit;

/**
 * @author:XWS
 * @Date:2020/8/1
 * @Description:com.itheima.controller
 */
@RestController
@RequestMapping("validateCode")
public class ValidateCodeController {
    @Autowired
    RedisTemplate redisTemplate;
    @RequestMapping("sendCodeToPhone.do")
    public Result sendCodeToPhone(@RequestParam("telephone") String telephone){
        try {
            //随机生成6位验证
            String code = ValidateCodeUtils.generateValidateCode(6);

            //创建jedis，将验证码和电话号码保存在redis中
//            Jedis jedis = jedisPool.getResource();
//            jedis.setex(telephone,90,code);
//            jedis.close();
            redisTemplate.opsForValue().set(telephone,code,90, TimeUnit.SECONDS);
//            SMSUtils.sendCode(telephone,code);
            System.out.println("---send code ==="+code);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
    @RequestMapping("/validateCodeFromRedis.do")
    public Result validateCodeFromRedis(@RequestParam("telephone")String telephone,@RequestParam("validateCode")String validateCode){
        try {
//            Jedis jedis = jedisPool.getResource();
//            String redisCode = jedis.get(telephone);
            String redisCode = (String) redisTemplate.opsForValue().get(telephone);
            if (redisCode==null||redisCode.length()==0){
                return new Result(false,"验证码过期");
            }
            if (!redisCode.equals(validateCode)){
                return new Result(false,"验证码输入错误");
            }

            return new Result(true, "验证码输入正确");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
    @RequestMapping("sendCodeForLogin.do")
    public Result sendCodeForLogin(@RequestParam("telephone") String telephone){
        try {
            //随机生成6位验证
            String code = ValidateCodeUtils.generateValidateCode(6);
            //创建jedis，将验证码和电话号码保存在redis中
//            Jedis jedis = jedisPool.getResource();
//            jedis.setex(telephone,90,code);
            redisTemplate.opsForValue().set(telephone,code,90, TimeUnit.SECONDS);
//            jedis.close();
            SMSUtils.sendCode(telephone,code);
            System.out.println("---send code ==="+code);
            return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
